<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Privilege;
use app\admin\model\Admin as ad;
use app\admin\model\AdminRole;
use app\admin\model\RolePrivilege;
use app\admin\model\Role;
use think\facade\Session;
use think\facade\Log;

class Admin extends Controller
{   
    
    //调用前置中间件的发放
    protected $middleware = [
        'Before'=> ['only' 	=> ['admin_list','admin_role','admin_cate','admin_rule'] ],
    ];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    //管理员列表
    public function admin_list(Request $request)
    {   
        // var_dump($request->hello);die;
        Log::write(Session::get('admin_name').'进入管理员列表','info');

        $list = AdminRole::where(1)->alias('a')->field('b.*,GROUP_CONCAT(c.role_name) as role_name')->join('admin b','a.admin_id = b.id')->join('role c','a.role_id = c.id')->group('b.id')->paginate(5);

        return $this->fetch('admin-list',['list'=>$list]);
    }
    //角色管理  角色权限关联表作为主表 关联角色以及权限
    public function admin_role()
    {   
        $list = RolePrivilege::where(1)->alias('a')->field('b.*,GROUP_CONCAT(c.pri_name) as role_list')->join('role b','a.role_id = b.id')->join('privilege c','a.pri_id = c.id')->group('b.id')->paginate(5);
        return $this->fetch('admin-role',['list'=>$list]);  
    }
    //权限分类
    public function admin_cate(Request $req)
    {   
        //检查是否有增加分类的值 若有往privilege表插入 parent_id=0的权限分类

        if(isset($req->cate_name)){
            $pri           = new Privilege;
            $pri->pri_name     = $req->cate_name;
            $pri->parent_id    = 0;
            $pri->url_path = ''; 
            $pri->save();
        }

        //取出所有权限分类 权限模块
        $list = Privilege::where('parent_id',0)->paginate(5);
        return $this->fetch('admin-cate',['list'=>$list]);
    }

    //权限管理
    public function admin_rule(Request $req)
    {   
        if(isset($req->cateid)&&isset($req->url1)&&isset($req->url2)&&isset($req->cate_name)){
            $pri           = new Privilege;
            $pri->pri_name     = $req->cate_name;
            $pri->parent_id    = $req->cateid;
            $pri->url_path = '/admin/'.$req->url1.'/'.$req->url2; 
            $pri->save();
        }
        // var_dump($req->cateid,$req->url1,$req->url2,$req->cate_name);die;
        $cate = Privilege::where('parent_id',0)->select();
        $list = Privilege::where('a.parent_id',0)->alias('a')->field('a.pri_name as parent_name,b.*')->join('privilege b','a.id = b.parent_id')->paginate(10);
        // echo '<pre>';
        // var_dump( $list);die;

        return $this->fetch('admin-rule',['list'=> $list,'cate'=>$cate]);
    }

    //管理员添加页面
    public function admin_add()
    {   
        //获取现有的所有的角色
        $list = Role::select();

        return $this->fetch('admin-add',['list'=>$list]);
    }
    /**
     * 保存新建的资源 添加管理员
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function admin_save(Request $req)
    {   
        // echo '<pre>';
        // var_dump($_POST);die;
        $admin = new ad;
        $admin->username     = $req->username;
        $admin->password    = md5($req->password);
        $admin->phone = $req->phone;
        $admin->email = $req->email;
        $admin->save();
        // 获取自增ID
        $admin_id =  $admin->id;

        $role = $req->role;

        for($i=0;$i<count($role);$i++){
            $admin_role = new  AdminRole;
            $admin_role->role_id = $role[$i];
            $admin_role->admin_id =  $admin_id;
            $admin_role->save();
        }
    }
    //管理员修改页面
    public function admin_edit()
    {
        return $this->fetch('admin-edit');
    }
    //角色添加
    public function role_add()
    {   //获取所有的权限
        $list = Privilege::select();
        return $this->fetch('role-add',['list'=>$list]);
    }
      /**
     * 保存新建的资源 添加角色
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function role_save(Request $req)
    {   
        $role = new Role;
        $role->role_name = $req->role_name;
        $role->desc = $req->desc;
        $role->save();
        //获取角色表自增id
        $role_id = $role->id;

        $privilege = $req->id;

        for($i=0;$i<count($privilege);$i++){
            $role_privilege = new RolePrivilege;
            $role_privilege->role_id = $role_id;
            $role_privilege->pri_id = $privilege[$i];
            $role_privilege->save();
        }


        // var_dump($_POST);die;


    }
    //角色修改
    public function role_edit()
    {
        return $this->fetch('role-edit');
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
